package team.dmh.wx.controller;


import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.dmh.wx.enums.FriendRequestTypeEnum;
import team.dmh.wx.enums.MsgOpsTypeEnum;
import team.dmh.wx.netty.ChatHandler;
import team.dmh.wx.netty.Content;
import team.dmh.wx.service.IFriendRequestService;
import team.dmh.wx.service.IFriendService;
import team.dmh.wx.util.Result;
import team.dmh.wx.util.ResultUtils;
import team.dmh.wx.vo.FriendVO;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hzk
 * @since 2018-10-15
 */
@Api(tags = "好友请求处理和查询")
@RestController
@Slf4j
@RequestMapping("/user")
public class FriendRequestController {


    @Autowired
    private IFriendService friendService;

    @Autowired
    private IFriendRequestService friendRequestService;


    @ApiOperation("请求添加好友")
    @PostMapping("/addFriend")
    public Result addFriend(@RequestBody @ApiParam(value = "userId(用户个人id)、friendUsername(需要查询的用户的用户名)") Map<String, String> params) {
        String userId = params.get("userId");
        String friendUsername = params.get("friendUsername");
        friendService.check(userId, friendUsername);
        Result result=new Result();
        if (friendRequestService.addFriend(userId, friendUsername)){
            result.success().put("type" ,1);
            return result;
        }
        return ResultUtils.success();
    }

    @ApiOperation("查询添加好友请求列表")
    @GetMapping("/queryFriendRequests")
    public Result queryFriendRequests(@ApiParam(value = "用户个人id") @RequestParam("userId") String userId) {
        return ResultUtils.success(friendRequestService.getFriendRequestVOList(userId));
    }

    @ApiOperation(("好友请求审核"))
    @PostMapping("/checkFriendRequest")
    public Result checkFriendReq(@RequestBody @ApiParam(value = "accepterId(请求接受者id)、senderId(请求发送者id)、type(请求当前状态 0 忽略 1 接受 2 未处理)") Map<String, String> params) {
        String accepterId = params.get("accepterId");
        String senderId = params.get("senderId");
        Integer type = Integer.valueOf(params.get("type"));
        if (type == FriendRequestTypeEnum.IGNORE.type) {
            friendRequestService.ignore(accepterId, senderId);
            return ResultUtils.success(friendService.getFriendVOList(accepterId)).info("忽略操作成功！");
        } else if (type == FriendRequestTypeEnum.PASS.type) {
            friendRequestService.pass(accepterId, senderId);
            Result result = new Result().success().data(friendService.getFriendVOList(accepterId)).info("添加操作成功！");
            FriendVO friendVO = friendService.getFriendInfo(accepterId,senderId);
            result.put("newFriend", friendVO);
            Channel channel1 = ChatHandler.users.get(senderId);
            if (ChatHandler.clients.contains(channel1)) {
                if (channel1 != null) {
                    channel1.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(
                            new Content()
                                    .setType(MsgOpsTypeEnum.PULL_FRIEND.type)
                                    .setExtend(
                                            new FriendVO()
                                                    .setFriendId(friendVO.getFriendId())
                                                    .setFriendAlias(friendVO.getFriendAlias())
                                                    .setFriendImg(friendVO.getFriendImg())
                                                    .setFriendNickname(friendVO.getFriendNickname())
                                                    .setFriendUsername(friendVO.getFriendUsername())
                                                    .setFriendSignature(friendVO.getFriendSignature()))


                    )));
                }
            }

            return result;
        } else {
            return ResultUtils.fail("错误的操作类型");
        }
    }
            //推送
            /*Channel channel1 = ChatHandler.users.get(accepterId);
            if (ChatHandler.clients.contains(channel1)) {
                if (channel1 != null) {
                    channel1.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(
                            new Content()
                                    .setType(MsgOpsTypeEnum.CHAT.type)
                                    .setChatMsgInput(
                                            new ChatMsgInput()
                                                .setMsg("我通过了你的朋友验证请求，现在我们可以开始聊天了")
                                                .setSenderId(senderId)
                                                .setAccepterId(accepterId)
                                                    //保存设置id  需要保存数据库
                                                    .setMsgId(IDUtils.getInstance().nextId())
                                                .setSenderAlias(chatMsgService.getSenderAlias(accepterId, senderId))
                                                ))));
                }
            }
            Channel channel2 = ChatHandler.users.get(senderId);
            if (ChatHandler.clients.contains(channel2)) {
                if (channel2 != null) {
                    channel2.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(
                            new Content()
                                    .setType(MsgOpsTypeEnum.CHAT.type)
                                    .setChatMsgInput(
                                            new ChatMsgInput()
                                                    .setMsg("我通过了你的朋友验证请求，现在我们可以开始聊天了")
                                                    .setSenderId(accepterId)
                                                    .setAccepterId(senderId)
                                                    .setMsgId(IDUtils.getInstance().nextId())
                                                    .setSenderAlias(chatMsgService.getSenderAlias(senderId, accepterId))
                                    ))));
                }
            }*/

}
